import { orderCancel, cancelOrderRefund, orderConfirmReceiving, prepaymentPay } from "../api/order";

// max: 21
const orderBtns = [
  {
    id: 1,
    name: '立即付款',
    status: [2]
  },
  {
    id: 2,
    name: '取消订单',
    status: [2, 3]
  },
  {
    id: 3,
    name: '确认收货',
    status: [20]
  },
  {
    id: 4,
    name: '查看账单',
    status: [10,30, 39]
  },
  {
    id: 5,
    name: '取消归还',
    status: [41, 43]
  },
  {
    id: 6,
    name: '查看归还物流',
    status: [44]
  },
  {
    id: 7,
    name: '再次下单',
    status: [45, 62, 60, 1, 13, 61]
  },
  {
    id: 8,
    name: '取消续租',
    status: [50, 51]
  },
  {
    id: 9,
    name: '申请退款',
    status: [9, 10, 13]
  },
  {
    id: 10,
    name: '申请归还',
    status: [30, 39]
  },
  {
    id: 11,
    name: '添加物流',
    status: [40, 42]
  },
  {
    id: 12,
    name: '再次申请',
    status: [43, 52]
  },
  {
    id: 13,
    name: '立即下单',
    status: [51]
  },
  {
    id: 14,
    name: '申请买断',
    status: [30, 39, 40]
  },
  {
    id: 15,
    name: '申请续租',
    status: [40]
  },
  {
    id: 16,
    name: '归还地址',
    status: [40, 42]
  },
  {
    id: 18,
    name: '取消退款',
    status: [11, 13]
  },
  {
    id: 19,
    name: '查看物流',
    status: [20, 30, 32, 39]
  },
  {
    id: 20,
    name: '签署合同',
    status: [3, 9, 10, 20]
  },
  {
    id: 22,
    name: '重新签约',
    status: [10, 20, 30, 39, 40]
  },
  {
    id: 17,
    name: '联系客服',
    status: [10, 11, 20, 30, 39, 40, 41, 42, 43, 44, 45, 50, 51, 52, 62, 60, 1, 61,102,16]
  },
]

function getBtnsByStatus(res) {
  const { status: order_status, sign_status, order_from, pay_type, pact_type, member_id, is_show } = res;
  // console.log('getBtnsByStatus', res)
  let result = [];
  if (order_from === 'qnhlapp') {
    // 判断千年国医
    const hideList = [2, 8, 9, 18, 14, 15];
    result = orderBtns.filter(item => item.status.indexOf(order_status) > -1 && hideList.indexOf(item.id) === -1);
  } else {
    result = orderBtns.filter(item => item.status.indexOf(order_status) > -1);
  }
  // 有签署合同按钮判断是否签约完成
  const index = result.findIndex(k => k.id == 20);
  if (index > -1 && sign_status == 2) {
    result.splice(index, 1);
  }
  // 判断签约未支付是否可以付款
  const index2 = result.findIndex(k => k.id == 1);
  if (index2 > -1 && pay_type == 1 && order_status == 3) {
    result.splice(index2, 1);
  }
  // 判断是否显示归还相关按钮(租完即送)
  const index3 = result.findIndex(k => k.id == 5 || k.id == 6 || k.id == 10 || k.id == 11 || k.id == 16);
  if (index3 > -1 && pact_type == 2) {
    result.splice(index3, 1);
  }
  // 判断是否显示重新签约按钮
  const index4 = result.findIndex(k => k.id == 22);
  // if (index4 > -1 && (pay_type != 1 || member_id != 1601436707) ) {
  //if (index4 > -1 && pay_type != 1) {
  if (index4 > -1) {
    result.splice(index4, 1);
  }
  // 隐藏再次下单
  const index5 = result.findIndex(k => k.id == 7);
  if (index5 > -1 && is_show != 1) {
    result.splice(index5, 1);
  }
  return result;
}

// 未支付取消订单
function cancelOrder(order_id, callback) {
  orderCancel(order_id).then(res => {
    my.showToast({
      type: 'success',
      content: '操作成功',
      duration: 3000
    });
    callback();
  })
}

// 撤销退款申请
function cancelRefundApply(order_id, callback) {
  cancelOrderRefund(order_id).then(res => {
    my.showToast({
      type: 'success',
      content: '操作成功',
      duration: 3000
    });
    callback();
  })
}

// 确认收货
function confirmReceipt(order_id, callback) {
  orderConfirmReceiving(order_id).then(res => {
    my.showToast({
      type: 'success',
      content: '操作成功',
      duration: 3000
    });
    callback();
  })
}

// 签署成功主动支付第一期
function payFirstRent(orderId, callback) {
  prepaymentPay({ orderId, leaseId: '' }).then(res => {
    if (res.tradeNo) {
      my.tradePay({
        tradeNO: res.tradeNo,
        success: (res) => {
          if (res.resultCode == 9000) {
            callback();
          } else {
            my.showToast({
              type: 'fail',
              content: '支付失败，请稍后重试',
              duration: 1000,
            });
          }
        }
      });
    }
  })
}
  //去身份认证
function   Auth(e) {
  const {
    contract_type,
    order_id,
    goods_id,
    pay_type,
    sign_num
  } = e
  my.navigateTo({
    url: '../../authentication/authentication?orderId=' + order_id + '&goodsId=' + goods_id + '&payType=' + pay_type +
      '&contractType=' + contract_type + '&signNum=' + sign_num
  })
}
//签合同
function  isContract() {
  const that = this
  const {
    order_id
  } = that.data.goodsData
  //sml prod
  const env = 'prod'
  my.showLoading({
    content: '正在跳转合同...'
  });
  createContract(order_id).then(res => {
    console.log('调起合同', res)
    if (res.flowId) {
      my.hideLoading({
        page: that
      });
      my.setStorage({
        key: 'hasCreateSign',
        data: true
      });
      const {
        flowId,
        signerId
      } = res
      console.log('走到合同签名');
      my.ap.openURL({
        url: `${baseUrl}/2.html?openUrl=${res.contractUrl}&orderid=${order_id}`, // 请将 url 替换为有效的页面地址
        success: (res) => {
          my.redirectTo({
            url: `pages/user/orderpay?id=${orderId}&typetime=${true}`,
          });
        
        },
        fail: (err) => {
          console.log('openURL success', err)
        }
      });
    } else {}
  })
}

module.exports = { getBtnsByStatus, cancelOrder, cancelRefundApply, confirmReceipt, payFirstRent,Auth }